package com.example.boot.service

import com.example.boot.dto.UserDTO
import com.example.boot.mapper.UserMapper
import com.example.boot.repository.UserRepository
import com.example.boot.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class UserServiceImpl : UserService {
    @Autowired
    private lateinit var userRepository: UserRepository

    @Autowired
    private lateinit var userMapper: UserMapper

    override fun findUserById(id: Long): UserDTO {
        val user = userRepository.findUserById(id)
        val userDTO = userMapper.convertToDTO(user)
        return userDTO
    }

    override fun createUser(userDTO: UserDTO): UserDTO {
        if (userRepository.existsByName(userDTO.name)) {
            println("user existed.")
        }
        val user = userMapper.convertToDomain(userDTO)
        val saved = userRepository.save(user)
        return userMapper.convertToDTO(saved)
    }
}